package com.loto.insurance.mis.web.controller.ajax;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.loto.insurance.mis.web.authority.INSAuthority;
import com.loto.insurance.mis.web.db.mapper.PolicyInfoAirMapper;
import com.loto.insurance.mis.web.db.mapper.PolicyInfoTravelMapper;
import com.loto.insurance.mis.web.entity.ajax.GridJson;
import com.loto.insurance.mis.web.entity.ajax.PolicyInfo;
import com.loto.insurance.mis.web.enumeration.InsTypeEnum;
import com.loto.insurance.mis.web.enumeration.MenuEnum;
import com.loto.insurance.mis.web.enumeration.ResultTypeEnum;
import com.loto.insurance.mis.web.service.OrderInfoService;
import com.loto.insurance.mis.web.service.UserInfoService;
import com.loto.insurance.mis.web.entity.ajax.OrderInfo;
import com.loto.insurance.mis.web.entity.Pager;
import com.loto.utils.DateUtils;

@Controller
@SessionAttributes("ins_user")
public class OrderJsonController {
  @Autowired
  UserInfoService userInfoService;

  @Autowired
  OrderInfoService orderInfoService;

  @Autowired
  PolicyInfoAirMapper policyInfoAirMapper;

  @Autowired
  PolicyInfoTravelMapper policyInfoTravelMapper;

  @INSAuthority(authorityMenu = MenuEnum.ORDER_LIST, resultType = ResultTypeEnum.json)
  @RequestMapping(value = "/order/list.json", method = RequestMethod.POST)
  public ModelAndView orderList(
	  @RequestParam(value = "orderNo", required = false) String orderNo,
	  @RequestParam(value = "policyNo", required = false) String policyNo,
	  @RequestParam(value = "policyName", required = false) String policyName,
	  @RequestParam(value = "custName", required = false) String custName,
	  @RequestParam(value = "orderBegin", required = false) String orderBegin,
	  @RequestParam(value = "orderEnd", required = false) String orderEnd,
	  @RequestParam(value = "orderStatus", required = false, defaultValue = "-1") int orderStatus,
	  @RequestParam(value = "insName", required = false, defaultValue = "-1") int insName,
	  @RequestParam(value = "orderType", required = false, defaultValue = "-1") int orderType,
	  @RequestParam(value = "page", required = false, defaultValue = "1") int page,
	  @RequestParam(value = "rows", required = false, defaultValue = "10") int rows) {
	Map<String, Object> params = new HashMap<String, Object>();
	if (orderNo != null && !orderNo.isEmpty())
	  params.put("orderNo", orderNo);
	if (orderBegin != null && orderEnd != null
	    && DateUtils.isDate(orderBegin, "yyyy-MM-dd")
	    && DateUtils.isDate(orderEnd, "yyyy-MM-dd")) {
	  params.put("orderBegin", orderBegin);
	  params.put("orderEnd", orderEnd);
	}
	if (orderStatus > -1)
	  params.put("orderStatus", orderStatus);
	if (policyNo != null && !policyNo.isEmpty())
	  params.put("policyNo", policyNo);
	if (policyName != null && !policyName.isEmpty())
	  params.put("policyName", policyName);
	if (custName != null && !custName.isEmpty())
	  params.put("custName", custName);
	if (orderType > -1)
	  params.put("orderType", orderType);
	if (insName > -1)
	  params.put("insName", insName);

	Pager pager = new Pager();
	pager.setPageSize(rows);
	if (params.size() == 0) {
	  Calendar now = Calendar.getInstance();
	  String today = DateUtils.dateToNormalString(now.getTime());

	  params.put("orderBegin", today);
	  params.put("orderEnd", today);
	}
	params.put("pageSize", pager.getPageSize());
	params.put("startIndex", pager.getPageSize() * (page - 1));

	if (page > 1)
	  pager.setIndex(page);
	ArrayList<com.loto.insurance.mis.web.entity.OrderInfo> orderList = orderInfoService
	    .findAllOrders(params);

	ArrayList<OrderInfo> list = new ArrayList<OrderInfo>();
	for (com.loto.insurance.mis.web.entity.OrderInfo order : orderList) {
	  OrderInfo o = new OrderInfo();
	  o.setOrderCreatedOn(DateUtils.formatShortDateTime(order
		  .getOrderCreatedOn()));
	  o.setOrderId(order.getOrderId());
	  o.setOrderNo(order.getOrderNo());
	  o.setOrderPersonCount(order.getOrderPersonCount());
	  o.setOrderProductName(order.getOrderProductName());
	  o.setOrderProductTypeFid(order.getOrderProductTypeFid());
	  o.setOrderRealAmount(order.getOrderRealAmount());
	  o.setOrderSettleAmount(order.getOrderSettleAmount());
	  o.setOrderSubCustFid(order.getOrderSubCustFid());
	  o.setOrderSubCustName(order.getOrderSubCustName());
	  o.setStatusDesc(order.getStatusDesc());
	  o.setOrderCustFid(order.getOrderCustFid());
	  o.setCustName(order.getCustName());
	  list.add(o);
	}

	pager.setRowCount(orderInfoService.findAllOrdersCount(params));

	ModelAndView mav = new ModelAndView();

	GridJson<OrderInfo> gridJson = new GridJson<OrderInfo>();
	gridJson.setTotal(pager.getRowCount());
	gridJson.setRows(list);

	mav.addObject("result", gridJson);
	return mav;
  }

  @INSAuthority(authorityMenu = MenuEnum.ORDER_LIST, resultType = ResultTypeEnum.json)
  @RequestMapping(value = "/order/policy.json", method = RequestMethod.POST)
  public ModelAndView policyList(@RequestParam(value = "orderId") long orderId,
	  @RequestParam(value = "orderType") int orderType) {
	List<PolicyInfo> list = new ArrayList<PolicyInfo>();
	if (orderType == InsTypeEnum.AIR.getIndex()) {

	  List<com.loto.insurance.mis.web.entity.PolicyInfoAir> policyList = policyInfoAirMapper
		  .findPolicyInfoByOrderId(orderId);

	  for (com.loto.insurance.mis.web.entity.PolicyInfoAir policy : policyList) {
		PolicyInfo p = new PolicyInfo();
		p.setFeeStatusDesc(policy.getFeeStatusDesc());
		p.setPolicyBegin(DateUtils.formatShortDateTime(policy.getPolicyBegin()));
		p.setPolicyCount(policy.getPolicyCount());
		p.setPolicyEnd(DateUtils.formatShortDateTime(policy.getPolicyEnd()));
		p.setPolicyFeeStatusFid(policy.getPolicyFeeStatusFid());
		p.setPolicyId(policy.getPolicyId());
		p.setPolicyInsuredBirth(DateUtils.dateToNormalString(policy
		    .getPolicyInsuredBirth()));
		p.setPolicyInsuredCredentialNo(policy.getPolicyInsuredCredentialNo());
		p.setPolicyInsuredCredentialType(policy
		    .getPolicyInsuredCredentialType());
		p.setPolicyInsuredEmail(policy.getPolicyInsuredEmail());
		p.setPolicyInsuredGender(policy.getPolicyInsuredGender());
		p.setPolicyInsuredName(policy.getPolicyInsuredName());
		p.setPolicyInsuredTel(policy.getPolicyInsuredTel());
		p.setPolicyNo(policy.getPolicyNo());
		p.setPolicyParam(policy.getPolicyFlightNo());
		p.setPolicyStatusFid(policy.getPolicyStatusFid());
		p.setStatusDesc(policy.getStatusDesc()
		    .equals(policy.getFeeStatusDesc()) ? policy.getStatusDesc()
		    : policy.getStatusDesc() + "," + policy.getFeeStatusDesc());
		list.add(p);
	  }
	} else if (orderType == InsTypeEnum.TRAVEL.getIndex()) {

	  List<com.loto.insurance.mis.web.entity.PolicyInfoTravel> policyList = policyInfoTravelMapper
		  .findPolicyInfoByOrderId(orderId);

	  for (com.loto.insurance.mis.web.entity.PolicyInfoTravel policy : policyList) {
		PolicyInfo p = new PolicyInfo();
		p.setFeeStatusDesc(policy.getFeeStatusDesc());
		p.setPolicyBegin(DateUtils.formatShortDateTime(policy.getPolicyBegin()));
		p.setPolicyCount(policy.getPolicyCount());
		p.setPolicyEnd(DateUtils.formatShortDateTime(policy.getPolicyEnd()));
		p.setPolicyFeeStatusFid(policy.getPolicyFeeStatusFid());
		p.setPolicyId(policy.getPolicyId());
		p.setPolicyInsuredBirth(DateUtils.dateToNormalString(policy
		    .getPolicyInsuredBirth()));
		p.setPolicyInsuredCredentialNo(policy.getPolicyInsuredCredentialNo());
		p.setPolicyInsuredCredentialType(policy
		    .getPolicyInsuredCredentialType());
		p.setPolicyInsuredEmail(policy.getPolicyInsuredEmail());
		p.setPolicyInsuredGender(policy.getPolicyInsuredGender());
		p.setPolicyInsuredName(policy.getPolicyInsuredName());
		p.setPolicyInsuredTel(policy.getPolicyInsuredTel());
		p.setPolicyNo(policy.getPolicyNo());
		p.setPolicyParam(policy.getPolicyTravelLine());
		p.setPolicyStatusFid(policy.getPolicyStatusFid());
		p.setStatusDesc(policy.getStatusDesc()
		    .equals(policy.getFeeStatusDesc()) ? policy.getStatusDesc()
		    : policy.getStatusDesc() + "," + policy.getFeeStatusDesc());
		list.add(p);
	  }
	}

	ModelAndView mav = new ModelAndView();

	GridJson<PolicyInfo> gridJson = new GridJson<PolicyInfo>();
	gridJson.setRows(list);

	mav.addObject("result", gridJson);
	return mav;
  }
}
